using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Admin_Friends : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        
    }


    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        MultiView1.ActiveViewIndex = 1;

        //GridViewRow GVR = GridView1.SelectedRow;     
        Friend friCurrent = FriendManager.GetFriend(Convert.ToInt32(GridView1.SelectedValue));
        tbo_Name.Text = friCurrent.Name;
        tbo_Url.Text = friCurrent.URL;
        tbo_Display.Text = friCurrent.Display;
        tbo_Alt.Text = friCurrent.Alt;
        tbo_Discription.Text = friCurrent.Description;

        hfi_ID.Value = friCurrent.ID.ToString();

        //tbo_Name.Text = GVR.Cells[1].Text;
        //tbo_Url.Text = GVR.Cells[2].Text;
        //tbo_Display.Text = GVR.Cells[5].Text;
        //tbo_Alt.Text = GVR.Cells[3].Text;
        //tbo_Discription.Text = GVR.Cells[4].Text;

        //hfi_ID.Value = GVR.Cells[6].Text;


        

    }
    protected void but_NewCat_Click(object sender, EventArgs e)
    {
        Response.Redirect("linkcategories.aspx");
    }
    protected void but_Save_Click(object sender, EventArgs e)
    {

        if (GridView1.SelectedIndex > -1)
        {
            //update
            Friend friCurrent = new Friend();

            friCurrent.Alt = tbo_Alt.Text;
            friCurrent.Description = tbo_Discription.Text;
            friCurrent.Display = tbo_Display.Text;
            friCurrent.ID = Convert.ToInt32( hfi_ID.Value);
            friCurrent.Name = tbo_Name.Text;
            friCurrent.URL = tbo_Url.Text;

            FriendManager.UpdateFriend(friCurrent);

        }
        else
        {
            //INSERT
            Friend friCurrent = new Friend();

            friCurrent.Alt = tbo_Alt.Text;
            friCurrent.Description = tbo_Discription.Text;
            friCurrent.Display = tbo_Display.Text;
            friCurrent.Name = tbo_Name.Text;
            friCurrent.URL = tbo_Url.Text;

            FriendManager.InsertFriend(friCurrent);

        }

        GridView1.DataBind();
        clearFields();
        MultiView1.ActiveViewIndex = 0;
    }
    protected void but_New_Click(object sender, EventArgs e)
    {
        clearFields();
        MultiView1.ActiveViewIndex = 1;

    }

    private void clearFields()
    {
        GridView1.SelectedIndex = -1;
        tbo_Display.Text = "";
        tbo_Discription.Text = "";
        tbo_Alt.Text = "";
        tbo_Url.Text = "";
        hfi_ID.Value = "";
        hfi_Del_ID.Value = "";
        tbo_Name.Text = "";

      
       
    }
    

    
    
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {        
        hfi_Del_ID.Value = e.Keys[0].ToString();    
    }
    protected void SqlDataSource1_Deleted(object sender, SqlDataSourceStatusEventArgs e)
    {
        clearFields();
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            //on edit this is no delete button and this will chuck an exception if we try and get the button.
            if (e.Row.Cells[7].Controls.Count > 0)
            {
                LinkButton lbuDelete = (LinkButton)e.Row.Cells[7].Controls[0];
                lbuDelete.OnClientClick = "return confirm(\'Are you sure you want to delete? This can not be undone!\');";
            }// end of the check to see if there are any controls in the cell

            //// the following code finds the delete button cell 
            //for (int intCounter = 0; intCounter < e.Row.Cells.Count; intCounter++)
            //{
            //    try
            //    {
            //        LinkButton lbuDelete = (LinkButton)e.Row.Cells[intCounter].Controls[0];

            //        if (lbuDelete.Text.ToLower().StartsWith("d"))
            //        {
            //            Response.Redirect(intCounter.ToString());
            //            //lbuDelete.OnClientClick = "return confirm(\'Are you sure you want to delete? This can not be undone.\');";
            //        }
            //    }
            //    catch
            //    {
            //    }
            //}    
        }// end of the if 
    }
    protected void butCancel_Click(object sender, EventArgs e)
    {
        clearFields();
        MultiView1.ActiveViewIndex = 0;
    }
}
